#!/usr/bin/python3
# -*- coding: utf-8 -*-
# coding=utf-8
# Author: BillySir
from __future__ import print_function
import sys
from rule import *
from chess_board import *
from dimension import T2D

print_process = False  # 是否打印解题（试填）过程
width = 3  # 每行多少格


def run():
    """3阶幻方"""
    # 3阶幻方
    # 格子序号 规则序号  一种解
    #          (0)
    # 0, 1, 2, (1)  6,1,8
    # 3, 4, 5, (2)  7,5,3
    # 6, 7, 8, (3)  2,9,4
    # (7)(6)(5)(4)
    # 小括号内的数字是规则的序号

    n = 3
    # 初始化棋子
    chess_list = [Chess(v, v) for v in range(1, n * n + 1)]
    # 初始化棋盒
    chess_dist = {}
    for chess in chess_list:
        chess_dist[chess] = 1
    # print('chess_dist:' + str(chess_dist))
    basket = ChessBasket(chess_dist)

    # 初始化棋盘上的格子
    cells = [Cell(basket) for _ in range(n * n)]
    # 初始化棋盘
    board = ChessBoard(cells)
    board.print_process = print_process
    board.width = width
    board.height = width

    # 初始化规则
    total = (1 + n * n) * n / 2
    rules = [RuleSum(total, basket) for _ in range((n + 1) * 2)]

    T2D.board = board
    T2D.width = 3
    T2D.height = 3
    Rule.batch_bind_parents(rules, T2D.get_cells_list("-|/\\"))
    board.rules = rules

    # 启动求解
    board.guess()
    print("finished")
    print("guess count: %d" % basket.guess_count)
